﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class BlockBehaviour : MonoBehaviour
{
    GameObject occupiedTile = null;

    void Start()
    {
        SetCurrentOccupiedTile();
        SetStepTileOccupaid();
    }

    private void SetCurrentOccupiedTile()
    {
        Collider2D[] colliders = Physics2D.OverlapPointAll(transform.position);
        foreach (Collider2D item in colliders)
        {
            if (item.CompareTag(StaticDataManager.TILE_TAG))
            {
                occupiedTile = item.gameObject;
                break;
            }
        }
    }

    private void SetStepTileOccupaid()
    {
        if (occupiedTile)
        {
            occupiedTile.GetComponent<TileMonoBehaviour>().isOcupied = true;
        }
    }

//    private void Update()
//    {
//        if (!occupiedTile)
//        {
//            SetCurrentOccupiedTile();
//            SetStepTileOccupaid();
//        }
//    }

    private void OnDestroy()
    {
        if (occupiedTile)
        {
            occupiedTile.GetComponent<TileMonoBehaviour>().isOcupied = false;
        }
    }

    private void OnDrawGizmosSelected()
    {
        Vector2[] directions = new Vector2[] { Vector2.up, Vector2.down, Vector2.left, Vector2.right };

        Dictionary<Vector2, TileMonoBehaviour> nearbyTiles = TileHelper.GetNearbyTile(gameObject, 1f);

        foreach (Vector2 direction in directions)
        {
            if (!nearbyTiles.ContainsKey(direction))
            {
                continue;
            }

            if (Physics2D.OverlapPoint(nearbyTiles[direction].transform.position, LayerMask.GetMask(StaticDataManager.BLOCK_LAYER)))
            {
                continue;
            }

            GameObject target = GameObject.FindGameObjectWithTag(StaticDataManager.PLAYER_TAG);
            
            float angle = Vector2.SignedAngle((target.transform.position - nearbyTiles[direction].transform.position).normalized, Vector2.up);

            if (angle < 0)
            {
                angle += 360;
            }

            TileHelper.SetFacingDirectionByAngle(angle, out var tileFacingDirections);
            
            Gizmos.color = Color.cyan;
            Gizmos.DrawLine(nearbyTiles[direction].gameObject.transform.position, target.transform.position);

            Gizmos.color = Color.yellow;
            Gizmos.DrawWireCube(nearbyTiles[direction].gameObject.transform.position, Vector3.one);

            Gizmos.color = Color.red;

            foreach (Vector2 coverDirection in tileFacingDirections)
            {
                if (coverDirection == Vector2.zero)
                {
                    continue;
                }

                RaycastHit2D hit = Physics2D.Raycast(nearbyTiles[direction].gameObject.transform.position, coverDirection, .7f, LayerMask.GetMask(StaticDataManager.BLOCK_LAYER));

                if (hit.collider)
                {
                    Gizmos.DrawLine(hit.collider.transform.position, nearbyTiles[direction].gameObject.transform.position);
                }
            }
        }
    }
}
